package siteconfig

import (
	"fmt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"myblog/app/model"
	siteconfigModel "myblog/app/model/system/siteconfig"
	"myblog/app/service/system/siteconfig"
	siteconfigService "myblog/app/service/system/siteconfig"
	"myblog/app/utils/response"
	"os"
	"strconv"
	"time"
)

//展示页
func List(r *ghttp.Request) {
	//获取配置信息
	siteconfig, err := siteconfig.SelectList()
	if err != nil {
		fmt.Printf("%v", err)
	}
	temp := make(map[string]string)
	for _, v := range siteconfig {
		temp[v.Name] = v.Value
	}
	response.BuildTpl(r, "system/siteconfig/index.html").WriteTpl(g.Map{
		"siteconfig": temp,
	})
}

//修改数据
func Edit(r *ghttp.Request) {
	var req *siteconfigModel.EditReq
	//获取参数
	if err := r.Parse(&req); err != nil {
		response.ErrorResp(r).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).Log("站点配置", req).WriteJsonExit()
	}
	//保存栏目图片
	if file := r.GetUploadFile("sitethumb"); file != nil {
		curDir, err := os.Getwd()
		saveDir := curDir + "/public/upload/" //文件保存路径
		curdate := time.Now().UnixNano()
		file.Filename = strconv.FormatInt(curdate, 10) + ".png"
		name, err := file.Save(saveDir, true)
		if err != nil {
			r.Response.WriteExit(err)
		}
		req.Sitethumb = "/upload/" + name
	} else {
		req.Sitethumb = req.Oldimage
	}
	err := siteconfigService.Edit(req)
	if err != nil {
		response.ErrorResp(r).SetBtype(model.Buniss_Edit).SetMsg(err.Error()).WriteJsonExit()
	}
	response.SucessResp(r).SetBtype(model.Buniss_Edit).SetData(g.Map{"code": 0, "msg": "修改成功"}).WriteJsonExit()
}

//获取值
func GetValueByName(key string) string {
	value, _ := siteconfigService.GetValueByName(key)
	return value
}



